import 'package:dio/dio.dart';
import '../index.dart';
import 'http.dart';
import 'http_base.dart';

class HttpEConcurUtil {
  static final HttpEConcurUtil _instance = HttpEConcurUtil._internal();
  factory HttpEConcurUtil() => _instance;
  late Dio dio;
  // CancelToken cancelToken = CancelToken();
  bool isTimeOut = false;
  HttpEConcurUtil._internal() {
    /// BaseOptions、Options、RequestOptions 都可以配置参数，优先级别依次递增，且可以根据优先级别覆盖参数
    dio = Dio(HttpBaseUtil().initBaseOptions(AppApi.SERVER_API_URL));

    ///配置 https 证书
    // (dio.httpClientAdapter as HttpClientAdapter ).onHttpClientCreate =
    //     HttpBaseUtil().onHttpClientCreate;
    // 添加拦截器
    dio.interceptors.add(HttpBaseUtil().httpInterceptorsWrapper());
  }

  /// restful get 操作
  /// refresh 是否下拉刷新 默认 false
  /// noCache 是否不缓存 默认 true
  /// list 是否列表 默认 false
  /// cacheKey 缓存key
  /// cacheDisk 是否磁盘缓存
  Future get(String path,
      {Map<String, dynamic>? params,
      bool showLoading = false,
      bool addAuthorization = true}) async {
    return await HttpRequestUtil().get(path,
        dio: dio,
        params: params,
        showLoading: showLoading,
        // cancelToken: cancelToken,
        addAuthorization: addAuthorization);
  }

  /// restful post 操作
  Future post(String path,
      {dynamic data,
      Map<String, dynamic>? queryParameters,
      Options? options,
      bool showLoading = false,
      bool addAuthorization = true}) async {
    return await HttpRequestUtil().post(path,
        dio: dio,
        data: data,
        queryParameters: queryParameters,
        options: options,
        showLoading: showLoading,
        // cancelToken: cancelToken,
        addAuthorization: addAuthorization);
  }

  /// restful put 操作
  Future put(String path,
      {dynamic data,
      Map<String, dynamic>? queryParameters,
      Options? options,
      bool showLoading = false,
      bool addAuthorization = true}) async {
    return await HttpRequestUtil().put(
      path,
      dio: dio,
      data: data,
      // cancelToken: cancelToken,
      queryParameters: queryParameters,
      options: options,
    );
  }

  /// restful delete 操作
  Future delete(String path,
      {dynamic data,
      Map<String, dynamic>? queryParameters,
      Options? options,
      bool showLoading = false,
      bool addAuthorization = true}) async {
    return await HttpRequestUtil().delete(
      path,
      dio: dio,
      data: data,
      // cancelToken: cancelToken,
      queryParameters: queryParameters,
      options: options,
    );
  }

  /// restful patch 操作
  Future patch(String path,
      {dynamic data,
      Map<String, dynamic>? queryParameters,
      Options? options,
      bool showLoading = false,
      bool addAuthorization = true}) async {
    return await HttpRequestUtil().patch(
      path,
      dio: dio,
      data: data,
      // cancelToken: cancelToken,
      queryParameters: queryParameters,
      options: options,
    );
  }

  /// restful postForm 操作
  Future postForm(
    String path, {
    dynamic data,
    Map<String, dynamic>? queryParameters,
    Options? options,
  }) async {
    return await HttpRequestUtil().postForm(
      path,
      dio: dio,
      data: data,
      // cancelToken: cancelToken,
      queryParameters: queryParameters,
      options: options,
    );
  }

  /// restful postForm 操作
  Future postStream(
    String path, {
    dynamic data,
    Map<String, dynamic>? queryParameters,
    Options? options,
  }) async {
    return await HttpRequestUtil().postStream(
      path,
      dio: dio,
      data: data,
      // cancelToken: cancelToken,
      queryParameters: queryParameters,
      options: options,
    );
  }

  /// 上传文件，返回参数为 T
  /// [path]:请求地址
  /// [filePath]: 文件路径
  /// [params]：请求参数
  /// [success]：请求成功回调
  /// [error]：请求失败回调
  Future uploadFile<T>(String path, String filePath,
      {bool showLoading = true}) async {
    return await HttpRequestUtil().uploadFile(
      path,
      filePath,
      dio: dio,
      showLoading: showLoading,
      // cancelToken: cancelToken,
    );
  }
}
